package main;


import com.kcbsdk.AFetchType;
import com.kcbsdk.CSvrManage;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyManage.AOwnSpecialList;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.cjmkcs;
import com.kcbsdk.wpapper;
import com.kcbsdk.MyManage.AAuthCodeReceiveWay;
import com.kcbsdk.MyManage.AGender;
import com.kcbsdk.MyManage.AManager;
import com.kcbsdk.MyManage.ARegisterAction;
import com.kcbsdk.MyManage.ARegisterSymbol;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import static com.kcbsdk.MyTools.ACloudType.TYPE_KBA;


/**
 * Created by Administrator on 2017/4/5.
 */

public class MyManage implements wpapper {
    public CSvrManage mSvrManage;
    public Activity mActivity = null;
    public EventHandler mEventHandler = null;

    public AManager mAManager;
    public int g2AuthCodeTime;
    private long g4SaveTickCounter;
    private int g4AddCounter;
    public String gsAuthCodeFile;
    int gCloudType;

    public MyManage(){
        mSvrManage = new CSvrManage();
        mAManager = new AManager();
        selectCloudType(TYPE_KBA);
    }
    public void pollingTimer(){
        int methodType;
        for (int counter = 0; counter < 8; counter++){
            methodType = mSvrManage.getMethodType();
            if (methodType > 0){
                String message = mSvrManage.getMethodMessage(methodType);
                boolean isFail = ("ok".equals(message)) ? false : true;
                if (!isFail){
                    //MSTRING(String.format("MyManage methodType %08X", methodType));
                    switch (methodType & 0xffff0000){
                        case TOOLS_DBS_LIST_MONEY:
                            break;
                    }
                }
                mEventHandler.onComplete(methodType, isFail, message);
            }
            else {
                break;
            }
        }
        int newEvent = mSvrManage.pollingEvent();
        if (newEvent > 0){
            if ((newEvent & MANAGE_EVENT_NEED_RELOGIN) > 0){
                mSvrManage.eventNeedRelogin();
            }
            if ((newEvent & MANAGE_EVENT_USER_INFO) > 0){
                mSvrManage.eventUserInfo(mAManager);
            }
            mEventHandler.onComplete(0x00000000, false, "ok");
        }
        if (g2AuthCodeTime > 0){
            if ((System.currentTimeMillis() - g4SaveTickCounter) > g4AddCounter){
                g4AddCounter += 1000;
                g2AuthCodeTime -= 1;
                mSvrManage.g4EventMethod |= MANAGE_EVENT_TIME_AUTH_CODE;
            }
        }

    }
    public void Toast(String text){
        Toast.makeText(mActivity, text, Toast.LENGTH_LONG).show();
        MSTRING(text);
    }
    public void  setUiEventCallBack(Activity activity, EventHandler handler){
        mActivity = activity;
        mSvrManage.setMethodType();
        mEventHandler = handler;
        MREG.g4LastSelect = tMANAGE;

        //mSvrManage.checkLoginSvr();
//
//        int userID = cjmkcs.systemGetWpapperValue(wpMANAGE_BaseUserID);
//        boolean isLogined = cjmkcs.systemGetWpapperValue(wpMANAGE_CheckLogin) > 0 ? true :false;
//        MSTRING("checkLoginSvr userID: "+userID + " "+isLogined);
//        if (userID > 0){
//            int dateTime = cjmkcs.systemGetWpapperValue(wpMANAGE_BaseDateTime);
//            fetchUserInfo(userID, dateTime);
//        }
//        else {
//            g4EventMethod |= MANAGE_EVENT_NEED_RELOGIN;
//        }
    }
    public void selectCloudType(ACloudType aCloudType){
        gCloudType = aCloudType.ordinal();
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchAuthCode(){
        mSvrManage.fetchAuthCode(gCloudType);
    }
    public boolean isSucessFetchAuthCode(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_FETCH_AUTH_CODE){
            gsAuthCodeFile = mSvrManage.eventAuthCode();
            return true;
        }
        return false;
    }
// 登录帐号 ///////////////////////////////////////////////////////////////////////////////////////////////

    public void checkIsLogined(){
        mSvrManage.checkIsLogined(gCloudType);
    }

    public void loginSvr(String account, String password, String authCode){
        mSvrManage.loginSvr(gCloudType, account, password, authCode);
    }
    public void logoutSvr(){
        mSvrManage.logoutSvr(gCloudType);
    }

    public void modifyUserInfo(AManager aManager){
        int birthday = 0;
        int area = aManager.AreaData;
        int genderLanguage = aManager.aGender.ordinal();
        mSvrManage.modifyUserInfo(gCloudType, aManager.NickName, aManager.Mood, aManager.Recommend,birthday,area,genderLanguage);
    }
    public void modifyPassword(String oldPassword, String newPassword, String imgAuthCode){
        mSvrManage.modifyPassword(oldPassword, newPassword, imgAuthCode);
    }
    /*
     请求服务器给用户的手机或邮箱发送验证码
     参数 action - 用户动作 详细说明请看ARegisterAction
     参数 symbol - 标志 详细说明请看ARegisterSymbol
     参数 symbolValue - 标志对应的值 （如：邮箱、手机号码、kcid号、用户名）
     参数 imgAuthCode - 图形验证码
     参数 completion － 任务执行完成后的回调。执行成功后，extraMessage为nil；执行失败后，extraMessage为错误消息
     */
    public void requestAuthCode(ARegisterAction action, ARegisterSymbol symbol, AAuthCodeReceiveWay rcvType, String symbolValue, String imgAuthCode) {
        mSvrManage.requestAuthCode(action.ordinal(), symbol.ordinal(), rcvType.ordinal(), symbolValue, imgAuthCode);
    }
    public void verifyAuthCode(String authCode){
        mSvrManage.verifyAuthCode(authCode);
    }
    public void modifyMailPhone(String mailOrPhone, String authCode){
        mSvrManage.modifyMailPhone(mailOrPhone, authCode);
    }
    public void resetPassword(String newPassword, String authCode){
        mSvrManage.resetPassword(newPassword, authCode);
    }
    public void assignUewUserID() {
        mSvrManage.assignUewUserID();
    }
    public void registerNewUser(String password, AGender aGender, String authCode){
        mSvrManage.registerNewUser(gCloudType, password, aGender.ordinal(), authCode);
    }
    public void thirdPartyLogin(int type, String userID, String token){
        mSvrManage.thirdPartyLogin(type, userID, token);
    }
    public void thirdPartyNewUser(int type, AGender aGender, String userID, String token, String mixText, String imgAuthCode){
        mSvrManage.thirdPartyNewUser(type, aGender.ordinal(), userID, token, mixText, imgAuthCode);
    }
    public void payMoney(int kcid,int money){
        mSvrManage.payMoney(kcid, money);
    }
    public void autoAddMoney(int kcid,int money){
        mSvrManage.autoAddMoney(kcid, money);
    }
    public String getLastSaveAccount(int type) {
        return cjmkcs.systemGetWpapperString(wpMANAGE_SaveAccount);
    }
    public String getMsVerifyLimitDate() {
        return cjmkcs.systemGetWpapperString(wpMANAGE_LimitDate);
    }
    public String getMsVerifyValidMinute() {
        return cjmkcs.systemGetWpapperString(wpMANAGE_ValidMinute);
    }
     public void searchFriend(AFetchType aFetchType, String nameInfo, int areaData, int specGender){	// 查找KCID列表
        mSvrManage.searchFriend(gCloudType, aFetchType.ordinal(), nameInfo, areaData, specGender);
    }

    public void fetchOwnSpecialDbs(AFetchType aFetchType, AOwnSpecialList aOwnSpecialList){
        mSvrManage.fetchOwnSpecialList(gCloudType, (int)aFetchType.ordinal(), (int)aOwnSpecialList.ordinal());
    }

    /////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////
    public boolean isSucessLoginSvr(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_LOGIN_SVR){
//MDEBUG(0xb1);MDWORD(mSvrManage.g4EventMethod);
            int userID = cjmkcs.systemGetWpapperValue(wpMANAGE_BaseUserID);
            int dateTime = cjmkcs.systemGetWpapperValue(wpMANAGE_BaseDateTime);
            mSvrManage.fetchUserInfo(gCloudType, userID, dateTime);
            return true;
        }
        return false;
    }
    public boolean isSucessLogoutSvr(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_LOGOUT_SVR){
            return true;
        }
        return false;
    }
    public boolean isSucessModifyUserInfo(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_MODIFY_USER_INFO){
            return true;
        }
        return false;
    }
    public boolean isSucessModifyPassword(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_MODIFY_PASSWORD){
            return true;
        }
        return false;
    }
    public boolean isSucessRequestAuthCode(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_REGUEST_AUTH_CODE){
            return true;
        }
        return false;
    }
    public boolean isSucessVerifyAuthCode(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_VERIFY_AUTH_CODE){
            return true;
        }
        return false;
    }
    public boolean isSucessModifyMailPhone(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_MODIFY_MAIL_PHONE){
            return true;
        }
        return false;
    }
    public boolean isSucessResetPassword(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_RESET_PASSWORD){
            return true;
        }
        return false;
    }
    public boolean isSucessAssignUewUserID(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_ASSIGN_NEW_USER_ID){
            return true;
        }
        return false;
    }
    public boolean isSucessRegisterNewUser(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_REGISTER_NEW_USER){
            return true;
        }
        return false;
    }
    public boolean isSucessThirdPartyLogin(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_THIRD_LOGIN){
            return true;
        }
        return false;
    }
    public boolean isSucessRhirdPartyNewUser(int methodType){
        if ((methodType & 0xffff0000) == MANAGE_THIRD_NEW_USER){
            return true;
        }
        return false;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    public boolean isEventOnDestroy(){
        if ((mSvrManage.g4EventMethod & MANAGE_EVENT_ON_DESTROY) > 0){
            return true;
        }
        return false;
    }
    public boolean isEventLoginedSvr(){
        if ((mSvrManage.g4EventMethod & MANAGE_EVENT_USER_INFO) > 0){
            mSvrManage.g4EventMethod &= ~MANAGE_EVENT_USER_INFO;
            return true;
        }
        return false;
    }
    public boolean isEventNeedRelogin(){
//MDEBUG(0xd3);MDWORD(mSvrManage.g4EventMethod);
        if ((mSvrManage.g4EventMethod & MANAGE_EVENT_NEED_RELOGIN) > 0){
            mSvrManage.g4EventMethod &= ~MANAGE_EVENT_NEED_RELOGIN;
            return true;
        }
        return false;
    }
    public boolean isEventAuthCodeTimer(){
        if ((mSvrManage.g4EventMethod & MANAGE_EVENT_TIME_AUTH_CODE) > 0){
            mSvrManage.g4EventMethod &= ~MANAGE_EVENT_TIME_AUTH_CODE;
            return true;
        }
        return false;
    }

    public void MSTRING(String text){
        Log.d("MSYS", text);
    }

}
